<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"  %>
<fmt:setBundle basename="messages.messages"/>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<link rel="stylesheet" href="/Epics/jslib/global.css" />
<style type="text/css" media="all">@import "/Epics/jslib/annotation.css";</style>
<script type="text/javascript" src="/Epics/jslib/jquery-1.7.1.js"></script>
<script type="text/javascript" src="/Epics/jslib/jquery-ui-1.8.17.js"></script>
<script type="text/javascript" src="/Epics/jslib/jquery.annotate.js"></script>

<c:if test="${photo.notesAllowed}">
	<script language="javascript">
		var pid="<c:out value="${photo.id}" />";
		$(window).load(function() {
			$("#toAnnotate").annotateImage({
				getUrl: "/Epics/SNoteGet?photoId="+pid,
				saveUrl: "/Epics/SNoteSave?photoId="+pid,
				deleteUrl: "/Epics/SNoteDelete?photoId="+pid,
				editable: true
			});
		});
	</script>
</c:if>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><fmt:message key="siteName"/></title>
</head>

<body>
	<%@ include file="../Template/TMainLine.jsp" %>
	<%@ include file="../Template/TMessage.jsp" %>
	
	<h1>${photo.title}</h1>
	<!-- .${fn:replace(photo.location,'\\','/') } -->
	<img id="toAnnotate" alt="${photo.description}" src=".${fn:replace(photo.location,'\\','/') }"><br/><br/>
	<table>
		<tr>
			<td>Title:</td>
			<td>${photo.title}</td>
		</tr>
		<tr>
			<td>Description:</td>
			<td>${photo.description}</td>
		</tr>
		<tr>
			<td>Author:</td>
			<td><a href="SAuthorsProfile?userId=${album.user.id}">${album.user.firstName} ${album.user.lastName}</a></td>
		</tr>
		<tr>
			<td>Rating:</td>
			<td>${photo.rating}</td>
		</tr>
	</table>
	
	<table>
		<c:if test="${!empty user and currentUser.id!=user.id }">
			<tr><td>
			<c:choose>
			<c:when test="${empty favorites}">
				<form action="SPhotoAddFavorite" method="post" accept-charset="UTF-8">
					<input type="hidden" value="${photo.id}" name="photoId" />
					<input type="submit" value="Add to favorites" />
				</form>
			</c:when><c:otherwise>
				<p>Added to favorites</p>
			</c:otherwise>
			</c:choose>
			</td></tr>
		</c:if>
		
		<c:if test="${!empty user and currentUser.id==user.id }">
			<tr><td>
			<form action="SChangePhotoSettings" method="post" accept-charset="UTF-8">
				<input type="hidden" value="${photo.id}" name="photoId" />
				<input type="submit" value="Change photo" />
			</form>
			</td></tr>
		</c:if>
		<c:if test="${!empty user and currentUser.id==user.id}">
			<tr><td>
			<form action="SPhotoDelete" method="post" accept-charset="UTF-8">
				<input type="hidden" value="${photo.id}" name="photoId" />
				<input type="submit" value="Delete photo" />
			</form>
			</td></tr>
		</c:if>
	</table>
	
	<c:if test="${photo.tagsAllowed }">
		<c:if test="${!empty photo.tags }">
			<h3>Tags:</h3>
			<table>
				<c:forEach items="${photo.tags}" var="tag">
					<tr>
						<td>${tag.text}</td>
						<td>
						<c:if test="${!empty user and user.id==currentUser.id }">
							<form action="SPhotoRemoveTag" method="post" accept-charset="UTF-8">
								<input type="hidden" value="${photo.id}" name="photoId" />
								<input type="hidden" value="${tag.id}" name="tagId" />
								<input type="submit" value="Remove tag" /><br />
							</form>
						</c:if>
						</td>
					</tr>
				</c:forEach>
			</table>
		</c:if>
		<c:if test="${!empty user }">
			<form action="SPhotoAddTag" method="post" accept-charset="UTF-8">
				<table>
					<tr><td>
						<input type="hidden" value="${currentUser.id}" name="currentUserId" />
						<input type="hidden" value="${photo.id}" name="photoId" />
						Tag:</td>
						<td><input type="text" name="tag"/></td>
					</tr>
					<tr><td colspan="2"><input type="submit" value="Add tag" /></td></tr>
				</table>
			</form>
		</c:if>
	</c:if>
	
	<c:if test="${!empty user and user.id!=currentUser.id and userRatingExists==0}">
		<h3>Add rating:</h3>
		<form action="SPhotoAddRating" method="post">
			<input type="hidden" value="${photo.id}" name="id" />
			<select name="rate">
				<option value="1">1</option>
				<option value="2">2</option>
				<option value="3">3</option>
				<option value="4">4</option>
				<option value="5" selected>5</option>
			</select><br/>
			<input type="submit" value="Rate" />
		</form>
	</c:if>

	<c:if test="${!empty user and userRatingExists==1}">
		<p>Rating: You added rating.</p>
	</c:if>
	
	<!-- 
	<h3>Rates:</h3>
	<c:forEach items="${photo.ratings}" var="rate">
		<b>${rate.user.firstName} ${rate.user.lastName}</b>: ${rate.vote}<br />
	</c:forEach>
	 -->
	 
	<c:if test="${photo.commentsAllowed}">
		<c:if test="${!empty  photo.comment }">
			<h3>Comments:</h3>
			<table>
			<c:forEach items="${photo.comment}" var="comment">
				<tr>
					<td><b>${comment.title }</b></td>
				</tr>
				<tr>
					<td><a href="SAuthorsProfile?userId=${comment.user.id}"> ${comment.user.firstName} ${comment.user.lastName}</a></td>
				</tr>
				<tr>
					<td>${comment.contents}</td>
				</tr>
				<tr>
					<td><fmt:formatDate value="${comment.date }" pattern="dd/MM/yyyy HH:mm" /></td>
				</tr>
				<c:if test="${!empty user and ((user.id==currentUser.id) or (comment.user.id==user.id)) }">
					<tr><td colspan="2">
					<form action="SPhotoRemoveComment" method="post" accept-charset="UTF-8">
						<input type="hidden" value="${photo.id}" name="photoId" />
						<input type="hidden" value="${comment.id}" name="commentId" />
						<input type="submit" value="Remove comment" /><br />
					</form>
					</td></tr>
				</c:if>	
			</c:forEach>
			</table>
		</c:if>
		<c:if test="${!empty user }">
			<h3>Add comment:</h3>
			<form action="SPhotoAddComment" method="post" accept-charset="UTF-8">
				<input type="hidden" value="${photo.id}" name="photoId" />
				<input type="hidden" value="${currentUser.id}" name="currentUserId" />
				<table>
				<tr>
					<td>Title:</td>
					<td><input type="text" name="title"/></td>
				</tr>
				<tr>
					<td>Content:</td>
					<td><textarea name="content" rows="5" cols="40"></textarea></td>
				</tr>
				<tr>
					<td colspan="2"><input type="submit" value="Comment" /></td>
				</tr>
				</table>
			</form>
		</c:if>
		
	</c:if>
	
</body>
</html>